package com.example.flower_shop.repository;

import com.example.flower_shop.entity.ReviewLike;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface ReviewLikeRepository extends JpaRepository<ReviewLike, Long> {

    /**
     * 根据用户ID和评论ID查找点赞记录
     */
    Optional<ReviewLike> findByUserIdAndReviewId(Long userId, Long reviewId);

    /**
     * 根据评论ID查找所有点赞记录
     */
    List<ReviewLike> findByReviewId(Long reviewId);

    /**
     * 根据用户ID和评论ID列表批量查找点赞记录
     */
    List<ReviewLike> findByUserIdAndReviewIdIn(Long userId, List<Long> reviewIds);

    /**
     * 根据评论ID统计点赞数
     */
    long countByReviewId(Long reviewId);

    /**
     * 根据用户ID和评论ID删除点赞记录
     */
    void deleteByUserIdAndReviewId(Long userId, Long reviewId);

    boolean existsByUserIdAndReviewId(Long userId, Long reviewId);

    @Modifying
    @Query("DELETE FROM ReviewLike r WHERE r.review.id = ?1")
    void deleteByReviewId(Long reviewId);
}